import { } from 'pixi.js';
import { Ball } from "./ball";
import { ThingSizeModel } from './base/model';
import { Tool } from './base/tool';
import { Map } from './map';
import { Racket } from "./racket";

const speed = 5;
export class Init {
    private canvasW: number = -1;
    private canvasH: number = -1;
    private app: PIXI.Application;

    // 地图
    private map: Map;
    // 所有球集合
    private allBalls: Ball[] = [];
    // 挡板
    private racket: Racket;

    private fn = (delta: any) => this.compute(delta);;

    constructor(private dom: HTMLDivElement) {
        let width = this.canvasW = 1000;// this.dom.clientWidth;
        let height = this.canvasH = 800;// this.dom.clientHeight;

        this.app = new PIXI.Application({width, height,
            // backgroundColor: 0xf4f3ef,
            antialias: true, resolution: 1 });

        this.map = new Map(this.app);

        // 初始的一个小球
        let ball = new Ball(this.app);
        this.allBalls = [ball];

        this.racket = new Racket(this.app);

        this.dom.appendChild(this.app.view);//

        this.app.ticker.add(this.fn);
    }

    // 计算运动轨迹
    private compute(delta: any) {
        this.allBalls.forEach(curBall => {
            this.move(curBall, delta);
        });
    }

    private move(ball: Ball, delta: any, force = false) {
        let nextX = ball.ballHandler.position.x + delta * speed * ball.xDirect;
        let nextY = ball.ballHandler.position.y + delta * speed * ball.yDirect;

        if (force) {
            ball.ballHandler.position.x = nextX;
            ball.ballHandler.position.y = nextY;
            return;
        }
        let size = ball.getBallOffset(nextX, nextY);

        let hasChange = false;
        if (size.x1 < 0) {
            ball.xDirect = 1;
            hasChange = true;
        }
        else if (size.x2 > this.app.view.width) {
            ball.xDirect = -1;
            hasChange = true;
        }
        else if (size.y1 < 0) {
            ball.yDirect = -ball.yDirect;
            hasChange = true;
        }
        else if (size.y2 > this.app.view.height) {
            // 落子结束
            this.app.ticker.remove(this.fn);
            //alert('lose');
            return;
        }
        else if (this.racket.hasCollision(size)) {
            ball.yDirect = -ball.yDirect;
            hasChange = true;
        }
        else {
            // 监测墙体碰撞
            let ret = Tool.hasCollision(this.map.blockArr, this.map.wallSize, size);
            if (ret.xReach || ret.yReach) {
                if (ret.xReach) {
                    ball.xDirect = -ball.xDirect;
                }
                if (ret.yReach) {
                    ball.yDirect = -ball.yDirect;
                }
                hasChange = true;
            }
            else {
                // 监测
                let softRet = Tool.hasCollision(this.map.softBlockArr, this.map.wallSize, size);
                if (softRet.xReach || softRet.yReach) {
                    if (softRet.xReach) {
                        ball.xDirect = -ball.xDirect;
                    }
                    if (softRet.yReach) {
                        ball.yDirect = -ball.yDirect;
                    }
                    while (true) {
                        let index = this.map.softBlockArr.findIndex(item => item.x === softRet.item?.x && item.y === softRet.item.y);
                        if (index < 0) {
                            break;
                        }
                        this.map.softBlockArr.splice(index, 1);
                    }
                    this.map.refreshSoftWall();
                    hasChange = true;
                }
            }
        }

        if (hasChange) {
            this.move(ball, delta, true);
            return;
        }
        ball.ballHandler.position.x = nextX;
        ball.ballHandler.position.y = nextY;
    }
}